package com.whf15.o2o.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whf15.o2o.cache.JedisUtil;
import com.whf15.o2o.dao.AreaDao;
import com.whf15.o2o.entity.Area;
import com.whf15.o2o.exceptions.AreaOperationException;
import com.whf15.o2o.service.AreaService;





/** 
* @author 作者 :whfo 
* @version 创建时间：2021年4月26日 下午10:47:40 
* 类说明 
*/
@Service
public class AreaServiceImpl implements AreaService{
	@Autowired
	private AreaDao areaDao; 
	//区域类别是不常用的可添加到redis中
	@Autowired
	private JedisUtil.Keys jedisKeys;
	@Autowired
	private JedisUtil.Strings jedisStrings;
	
	
	private static Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);
//	@Override
//	@Transactional
//	public List<Area> getAreaList() {
//		//定义redis的key
//		String key = AREALISTKEY;
//		//定义接受对象
//		List<Area> areaList = null;
//		//定义json数据转换操作类
//		ObjectMapper mapper = new ObjectMapper();
//		//判断key是否存在
//		if(!jedisKeys.exists(key)) {
//			//若不存在，则从数据库里面取出相应数据
//			areaList = areaDao.queryArea();
//			//将相关的实体类集合转换成string，存入redis里面对应的key中
//			String jsonString;
//			try {
//				jsonString = mapper.writeValueAsString(areaList);
//			} catch (JsonProcessingException e) {
//				e.printStackTrace();
//				logger.error(e.getMessage());
//				throw new AreaOperationException(e.getMessage());
//			}
//			jedisStrings.set(key, jsonString);
//		}else {
//			String jsonString = jedisStrings.get(key);
//			JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Area.class);
//			try {
//				areaList = mapper.readValue(jsonString, javaType);
//			} catch (JsonParseException e) {
//				e.printStackTrace();
//				logger.error(e.getMessage());
//				throw new AreaOperationException(e.getMessage());
//			} catch (JsonMappingException e) {
//				e.printStackTrace();
//				logger.error(e.getMessage());
//				throw new AreaOperationException(e.getMessage());
//			} catch (IOException e) {
//				e.printStackTrace();
//				logger.error(e.getMessage());
//				throw new AreaOperationException(e.getMessage());
//			}
//		}
//// 		return areaDao.queryArea();
//		return areaList;
//	}
	@Override
	public List<Area> getAreaList() {
		return areaDao.queryArea();
	}

}
